Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  onLoad() {
    // 查看是否授权
    this.login();
  },
  
  bindGetUserInfo(e) {
    console.log(e.detail.userInfo);
    let that = this;
    this.login();
  },

  login () {
    wx.login ({
      success: res => {
        console.log(res);
        getApp().globalData.code = res.code;
        wx.getSetting({
          success(res) {
            console.log(res);
            if (res.authSetting['scope.userInfo']) {
              wx.getUserInfo({
                success(res) {
                  console.log('------------------------------------');
                  console.log(res);
                  getApp().globalData.userInfo = res.userInfo;
                  getApp().globalData.nickName = res.userInfo.nickName;
                  wx.request({
                    // url: 'http://192.168.3.31:8080/user/login',
                    url: 'https://localhost:80/user/login',
                    method: 'post',
                    data: {
                      code:  getApp().globalData.code,
                      nickName:  getApp().globalData.userInfo.nickName,
                      encryptedData: res.encryptedData,
                      iv: res.iv,
                      rawData: res.rawData,
                      signature: res.signature
                    },
                    success: res => {
                      console.log('获取用户信息成功');
                      getApp().globalData.userID = res.data.userId;
                      getApp().globalData.openID = res.data.userOpenid; 
                      wx.setStorage({
                        key: 'userID',
                        data: res.data.userId
                      });
                      console.log(res);
                      console.log(getApp().globalData.userID);
                      console.log(getApp().globalData.openID)
                      console.log('78798')
                      wx.request({
                        url: 'https://localhost:80/admins/query',
                        data: {
                          adminId: res.data.userId
                        },
                        header: {
                          'content-type': 'application/json'
                        },
                        method: 'post',
                        success (res) { 
                          console.log('test--------------------------------test');
                          console.log(res.data);
                          getApp().globalData.vipState = res.data.adminRole;
                          wx.switchTab({
                            url: '../index'
                          })
                        }
                      });
                    },
                    error: res => {
                      wx.showModal({
                        title: '温情提示',
                        content: '亲，因为网络原因，请稍后重试',
                        text:'center',
                        complete() {}
                      });
                    }
                  })
                },
                fail (res) {
                  console.log('用户未授权');
                }
              })
            }
          }
        })
      }
    })
  }
})